const Promise = require('bluebird');
const moment = require('moment');

/**
 * @api {get} /api/admin/v1/finances/analyse 资金流水统计
 * @apiGroup AdminUsersFinances
 * @apiDescription 财务管理-资金流水统计
 * @apiPermission 管理员(查看资金流水权限)
 * @apiVersion 1.0.0
 */
exports.analyse = function ({}, {models: {UsersFinances}}) {
    return UsersFinances.analyseV2();
}

/**
 * @api {get} /api/admin/v1/finances/browse 资金流水列表
 * @apiGroup AdminUsersFinances
 * @apiDescription 财务管理-资金流水列表
 * @apiPermission 管理员(查看资金流水权限)
 * @apiVersion 1.0.0
 */
exports.browse = function ({query}, {models: {UsersFinances, Users, associations}, cache: {settings}, controller}) {
    let pagination = controller.pagination({
        params: query,
        filters: ['partner_id', 'user_id', 'type', 'operation', 'by', 'status', 'is_settled', 'is_amount'],
        likes: ['order_id', 'user_id', 'payment_no']
    });
    return UsersFinances.findAndCountAll(pagination).then(function (data) {
        return associations.relateOne(data, Users, 'user_id', 'user', ['id', 'image', 'nickname', 'username', 'nickname', 'tel']);
    }).then(function (data) {
        return associations.relateOne(data, Users, 'partner_id', 'partner_user', ['id', 'image', 'username', 'nickname', 'tel']);
    });
}